package qj.util;

import java.sql.Connection;
import java.sql.SQLException;

public class JdbcUtil {
	private static ThreadLocal<Connection> CONN_TL = new ThreadLocal<Connection>();

	/**
	 * Set the connection into Thread local variable
	 * @param conn Connection
	 */
	public static void setThreadConnection(Connection conn) {
		CONN_TL.set(conn);
	}
	
	/**
	 * 
	 * @return
	 */
	public static Connection getTConn() {
		return CONN_TL.get();
	}

	public static void commitAndClose(Connection conn) {
		try {
			conn.commit();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// Dont care
		}
	}

	public static void rollbackAndClose(Connection conn) {
		try {
			conn.rollback();
			conn.close();
		} catch (SQLException e) {
			// Dont care
		}
	}
}
